package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.ProfessDirectionDao;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ProfessDirectionActionImpl.java
* Description：专业方向actionImpl类,负责操作专业方向数据
* History：
* Date           Author               Desc
* 2017.4.12		  xzx                 建立
* -------------------------------------------------------
*/

@Component("professDirectionActionImpl")
@Transactional
public class ProfessDirectionActionImpl {

	@Resource(name="professDirectionDao")
	private ProfessDirectionDao professDirectionDao;

	@Resource(name = "courseActionImpl")
	private CourseActionImpl courseActionImpl;

	@Resource(name = "departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;

	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ProfessDirectionActionImpl.class.getName());
	
	/**
	 * @description 添加专业方向
	 * @param Map<String,Object> paramMap  专业方向名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> addDirect(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			professDirectionDao.addDirect(paramMap);
			resultMap.put("result", true);
			logger.info("添加专业方向,参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("添加专业方向异常addDirect", e);
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 验证专业方向是否重名的
	 * @param paramMap 专业方向的集合
	 * @param mark 0传“”的column  1传directName的column
	 * */
	public Map<String, Object> getDirectRepeatMark(Map<String, Object> paramMap, int mark){

		Map<String, Object> resultMap = new HashMap<String,Object>();
		try {
			int trueLength = 0;
			//得到专业方向名字
			List<Map<String, Object>> directList = (List<Map<String, Object>>) paramMap.get("directions");
			String column = (mark == 0) ? "" : "directName";
			//判断重名问题
			if (!courseActionImpl.checkRepeatFunc(directList, column)) {
				resultMap.put("result", "fail");
				resultMap.put("failMsg", "专业方向名称重复");
				return resultMap;
			}
			//判断数据库重名问题
			Map<String, Object> searchParamMap ;
			List<Map<String, Object>> directListUse;
			Object directListClass;
			if(directList != null && directList.size() != 0){
				for(int i = 0; i < directList.size(); i++){

					directListClass = directList.get(i);
					if(directListClass instanceof String){
						searchParamMap = Maps.newHashMap();
						searchParamMap.put("directName", directList.get(i));
						directListUse = professDirectionDao.getDirectByName(searchParamMap);
					}else{
						directListUse = professDirectionDao.getDirectByName(directList.get(i));
					}

					//判断重复名
					if(directListUse != null && directListUse.size() != 0){
						break;
					}else{
						trueLength += 1;
					}
				}
				if(trueLength == directList.size()){
					resultMap.put("result", "success");
				}else{
					resultMap.put("result", "fail");
					resultMap.put("failMsg", "专业方向名称重复");
					return resultMap;
				}
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", "专业方向为空");
				return resultMap;
			}
		}catch(Exception e){
			logger.error("验证专业方向是否重名的工具类异常getDirectRepeatMark", e);
			e.printStackTrace();
		}
		return resultMap;
	}
	

	
	/**
	 * @description 删除专业方向(批量)
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delDirect(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> result = new HashMap<String, Object>();
		try{
			if(paramMap.get("delList") != null && ((List<Integer>)paramMap.get("delList")).size() != 0){
				for(Integer mainId : (List<Integer>)paramMap.get("delList")){
					result.put("mainId", mainId);
					//删除专业方向信息
					professDirectionDao.delDirect(result);
				}
				result.put("result", true);
			}else{
				result.put("result", true);
			}
			logger.info("删除专业方向，参数为："+paramMap.toString());
		}catch(Exception e){
			logger.error("删除专业方向异常delDirect", e);
			e.printStackTrace();
			result.put("result", false);
			throw e;
		}
		return result;
	}

	/**
	 * @description 获取删除专业方向的标志
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delDirectMark(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String[] tables = {"course","relation"};
			String[] errMsg = {"课程","课表"};
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap = departMentActionImpl.getDepartmentDelMark("专业方向",tables, errMsg, "mainId", Integer.parseInt(paramMap.get("mainId").toString()));
			boolean resultMark = (Boolean)resultMap.get("result");
			if(!resultMark){
				result.put("delMark", 1);
				result.put("result", "success");
				result.put("delMsg", "你所选择的专业方向含有课程信息，无法删除");
				return result;
			}else{
				result.put("delMark", 0);
				result.put("result", "success");
				return result;
			}
		}catch(Exception e){
			logger.error("获取删除专业方向标志异常delDirectMark", e);
			throw e;
		}
	}
	
	/**
	 * @description 查询某个专业的专业方向列表
	 * @param Map<String,Object> paramMap  专业id
	 * @return List<Map<String, String>> 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getDirectByprofessId(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = professDirectionDao.getDirectByprofessId(paramMap);
		}catch(Exception e){
			logger.error("查询某个专业的专业方向列表异常getDirectByprofessId", e);
			throw e;
		}
		return list;
	}
	
	/**
	 * @description 修改专业的专业方向
	 * @param Map<String,Object> paramMap  专业方向名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> saveDirectList(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
//			//1013过滤输入
//			List<Map<String, Object>> directsList = ParamConvertUtil.replaceHTMLCode((List<Map<String, Object>>)paramMap.get("directions"), "directName");
//			paramMap.put("directions", directsList);
			List<Map<String, Object>> paramDirectList = (List<Map<String, Object>>)paramMap.get("directions");
			List<Map<String, Object>> directList;
			for(int i = 0; i < paramDirectList.size(); i++){
				paramDirectList.get(i).put("professionId", paramMap.get("professionId"));
				//新增
				if(Integer.parseInt(paramDirectList.get(i).get("mainId").toString()) == 0){
					directList = professDirectionDao.getDirectByName(paramDirectList.get(i));
					if(directList != null && directList.size() != 0 ){
						resultMap.put("result", false);
						resultMap.put("failMsg", "专业方向名称重复");
						break;
					}else{
						professDirectionDao.addDirectOne(paramDirectList.get(i));
						resultMap.put("result", true);
					}
				//修改	
				}else{
					List<Map<String, Object>> directListSave = professDirectionDao.getDirectByName(paramDirectList.get(i));
					if(directListSave != null && directListSave.size() != 0 ){
						resultMap.put("result", false);
						resultMap.put("failMsg", "专业方向名称重复");
						break;
					}else{
						professDirectionDao.saveDirect(paramDirectList.get(i)); 
						resultMap.put("result", true);
					}
				}
			}
			logger.info("修改专业的专业方向,参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("修改专业的专业方向异常saveDirectList", e);
			throw e;
		}
		return resultMap;
	}


	/**
	 * @description 判断是否有不能删除的专业方向信息
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delDirectListMark(List<Integer> delList) throws Exception{

		Map<String, Object> result = new HashMap<String, Object>();
		int paramSize = delList.size();
		int delListSize = 0;
		StringBuffer cantMsg = new StringBuffer("");
		try{
			String[] tables = {"course","relation"};
			String[] errMsg = {"课程","课表"};
			Map<String, Object> resultMap = new HashMap<String, Object>();
			for(Integer mainId : delList){
				resultMap = departMentActionImpl.getDepartmentDelMark("专业方向",tables, errMsg, "mainId", mainId);
				if(!(Boolean)resultMap.get("result")){
					cantMsg.append(professDirectionDao.getDirectById(mainId).get("directName").toString()).append(",");
				}else{
					delListSize += 1;
				}
			}
			if(paramSize == delListSize){
				result.put("result", "success");
				return result;
			}else{
				result.put("result", "fail");
				result.put("failMsg", cantMsg.deleteCharAt(cantMsg.length() - 1).toString()+"专业方向存在关联信息，不可删除");
				return result;
			}
		}catch(Exception e){
			logger.error("判断是否有不能删除的专业方向信息异常delDirectListMark", e);
			e.printStackTrace();
			throw e;
		}
	}
	
}